package ca.jacobk.math;

import android.util.Log;

/**
 * Math functions to help work out how the wheel spins
 * @author arlen phillips
 *
 */
public class WheelTicker { 
		
	public static final float CIRCLE_IN_RADIANS = 6.28318531f;
	private float remainingSpin = 0 ;
	private float initialSpin =0; 
	private float extraSpins = 0;
	private float radiansPerTick = CIRCLE_IN_RADIANS / 40;
	private float minimumRadiansPerTick = CIRCLE_IN_RADIANS / 100;
	private float decay = CIRCLE_IN_RADIANS / 200;
	
	public static float radiansToSegment(int totalSegments, int targetSegment){
		return  targetSegment * (CIRCLE_IN_RADIANS / (float)totalSegments );
	}

	public void setExtraSpins(int spins){
		
		 this.extraSpins = CIRCLE_IN_RADIANS * (float)spins;
	}
	
	public void setGoalSegment(int totalSegments, int targetSegment){
		remainingSpin = extraSpins  + radiansToSegment(totalSegments, targetSegment);
	}
	
	/**
	 * Gets the current rotation that the wheel should be rendered at	
	 * @return
	 */
	public float getRotation(){
		return remainingSpin;
	}
	
	public void tick(){
		if(initialSpin ==0 )
		{
			initialSpin = remainingSpin;
		}
		remainingSpin -= radiansPerTick;
		
		Log.d("Percent",remainingSpin / initialSpin * 100 + " %");
		if(remainingSpin / initialSpin < .10){
			if(radiansPerTick > minimumRadiansPerTick)
			{
				radiansPerTick -= decay;
			}
		}
		
		if(remainingSpin < 0){
			remainingSpin = 0;
		}
	}
}
